package org.leaf.green.b2b

class RetailerController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def retailerService

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        render view : '/b2b/retailer/list', model : [retailerInstanceList: Retailer.list(params), retailerInstanceTotal: Retailer.count()]
    }

    def create = {
        def retailerInstance = new Retailer()
        retailerInstance.properties = params
        render view : '/b2b/retailer/create', model : [retailerInstance: retailerInstance]
    }

    def save = {
        def retailerInstance = new Retailer(params)
        if (!retailerInstance.hasErrors() && retailerInstance.save()) {
            flash.message = "retailer.created"
            flash.args = [retailerInstance.id]
            flash.defaultMessage = "Retailer ${retailerInstance.id} created"
            redirect(action: "show", id: retailerInstance.id)
        }
        else {
            render(view: "/b2b/retailer/create", model: [retailerInstance: retailerInstance])
        }
    }

    def show = {
        def retailerInstance = Retailer.get(params.id)
        if (!retailerInstance) {
            flash.message = "retailer.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Retailer not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/retailer/show', model : [retailerInstance: retailerInstance]
        }
    }

    def inventories = {
        def retailerId = params.retailer.toLong()
        def inventories = retailerService.inventories(retailerId)
        render view : '/b2b/retailer/inventories', model : [retailerInventoryInstanceList: inventories]
    }

    def edit = {
        def retailerInstance = Retailer.get(params.id)
        if (!retailerInstance) {
            flash.message = "retailer.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Retailer not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/retailer/edit', model : [retailerInstance: retailerInstance]
        }
    }

    def update = {
        def retailerInstance = Retailer.get(params.id)
        if (retailerInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (retailerInstance.version > version) {
                    
                    retailerInstance.errors.rejectValue("version", "retailer.optimistic.locking.failure", "Another user has updated this Retailer while you were editing")
                    render(view: "/b2b/retailer/edit", model: [retailerInstance: retailerInstance])
                    return
                }
            }
            retailerInstance.properties = params
            if (!retailerInstance.hasErrors() && retailerInstance.save()) {
                flash.message = "retailer.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Retailer ${params.id} updated"
                redirect(action: "show", id: retailerInstance.id)
            }
            else {
                render(view: "/b2b/retailer/edit", model: [retailerInstance: retailerInstance])
            }
        }
        else {
            flash.message = "retailer.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Retailer not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def retailerInstance = Retailer.get(params.id)
        if (retailerInstance) {
            try {
                retailerInstance.delete()
                flash.message = "retailer.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Retailer ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "retailer.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Retailer ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "retailer.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Retailer not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
